using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class admin_SpotCategoryManagement : admin_BaseUserControl
{
    SpotCategoryDAO daoSpotCate = new SpotCategoryDAO();
    SpotCategory cate = new SpotCategory();
    protected void Page_Load(object sender, EventArgs e)
    {        
        if (Request["mode"] == "1")
        {
            lnkAdd.Visible = false;
            lnkUpdate.Visible = true;
            lnkDelete.Visible = true;
            try
            {
                int id = int.Parse(Request["id"]);
                CurrentID = Request["id"];
                cate = daoSpotCate.GetSpotCategory(id);
                txtName.Text = cate.Name;
                txtDescription.Text = cate.Description;
                chkState.Checked = cate.State;
                FCKeditor1.Value = cate.Article;
            }
            catch (Exception ex)
            {
                Response.Redirect("Default.aspx?ctrl=spot_cate_list");
            }
        }
        else
        {
            txtName.Text = "";
            txtDescription.Text = "";
            FCKeditor1.Value = "";
            chkState.Checked = false;
            lnkAdd.Visible = true;
            lnkUpdate.Visible = false;
            lnkDelete.Visible = false;
        }
    }
    protected void lnkAdd_Click(object sender, EventArgs e)
    {
        cate.Name = txtName.Text;
        cate.Description = txtDescription.Text;
        cate.State = chkState.Checked;
        cate.Article = FCKeditor1.Value;
        cate.ModifiedBy = 1;
        cate.ModifiedDate = DateTime.Now;
        if (daoSpotCate.Insert(cate) > 0)
        {
            ShowMessage("New spot category has been created successfully");
            lnkAdd.Visible = false;
            lnkUpdate.Visible = true;
            lnkDelete.Visible = true;
            CurrentID = cate.Id.ToString();
        }
        else ShowMessage("Error occured when trying to create new spot category");

    }
    protected void lnkUpdate_Click(object sender, EventArgs e)
    {
        int id = int.Parse(CurrentID);       
        cate.Id = id;
        cate.Name = txtName.Text;
        cate.Description = txtDescription.Text;
        cate.State = chkState.Checked;
        cate.Article = FCKeditor1.Value;
        cate.ModifiedBy = 1;
        cate.ModifiedDate = DateTime.Now;
        if (daoSpotCate.Update(cate) > 0)
        {
            ShowMessage("Spot category has been updated successfully");
        }
        else ShowMessage("Error occured when trying to update current spot category");
    }
    protected void lnkDelete_Click(object sender, EventArgs e)
    {
        try
        {
            int id = int.Parse(CurrentID);
            if (daoSpotCate.Delete(id) > 0)
            {
                ShowMessage("The category is deleted successfully");
                lnkUpdate.Visible = false;
                lnkDelete.Visible = false;
                lnkAdd.Visible = true;
                txtName.Text = "";
                txtDescription.Text = "";
                FCKeditor1.Value = "";
            }
            else ShowMessage("Error occured when trying to delete current category");
        }
        catch (Exception ex)
        {
            ShowMessage("Error occured when trying to delete current category");
        }
}
}
